﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using zdzh.zdmgr.BLL;
using zdzh.zdmgr.COL;
using zdzh.zdmgr.SFL;

namespace zdzh.zdmgr.UIL.fangyuanp
{
    /// <summary>
    /// 对象名称：“回迁协议模块”修改信息页面（用户界面层）
    /// 对象说明：该页面用于修改“回迁协议”对象并更新至数据库，操作完成后返回列表页面。
    /// 作者姓名：飞翔熊
    /// 编写日期：2012/12/3 13:23:16
    /// </summary>
    public partial class Detail : System.Web.UI.Page
    {
        protected Fangyuan fangyuan;
        protected Dingdan dingdan;
        protected Huiqianxieyi huiqianxieyi;
        protected List<Huiqianhu> huiqianhu;
        protected List<Huiqiandianhua> dianhua;
        protected Yuangong yuangong;
        protected List<Wuyefei> wuyefei;


        /// <summary>
        /// “回迁协议模块”修改信息页面载入时的事件处理方法。
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            fangyuan = FangyuanBLL.GetDataByIndex(Request.QueryString["Index"]); // 从数据库中获取关联对象信息，以备进行修改操作。
            if (fangyuan == null)
                throw new CustomException("对不起，所要查询的信息不存在或已删除。");
            if ( fangyuan.XieyiIndex ==null || fangyuan.XieyiIndex == 0)
            {
                BtnDingfang.Visible = true;
                BtnTuifang.Visible = false;
            }
            else
            {
                BtnDingfang.Visible = false;
                BtnTuifang.Visible = true;
                if ( fangyuan.State > 0)
                {
                    huiqianhu = HuiqianhuBLL.GetDataByXieyihao(fangyuan.XieyiIndex.Value);
                    dianhua = HuiqiandianhuaBLL.GetDataByXieyihao(fangyuan.XieyiIndex.Value);

                    if (fangyuan.State == 2 || fangyuan.State == 4)
                    {
                        dingdan = DingdanBLL.GetDataByIndex(Convert.ToInt32(fangyuan.XieyiIndex));
                        yuangong = YuangongBLL.GetDataByIndex(dingdan.SellerIndex.Value);

                        TblDingdan.Rows[2].Cells[0].Text = "<a href=\"../dingdanp/Modify.aspx?Index=" + dingdan.Index.ToString() + "\">" + dingdan.Index.ToString()+"</a>";
                        TblDingdan.Rows[2].Cells[1].Text = dingdan.DutDate.Value.ToString("yyyy-MM-dd");
                        TblDingdan.Rows[2].Cells[2].Text = dingdan.Number;
                        TblDingdan.Rows[2].Cells[3].Text = "";
                        foreach (Huiqianhu model in huiqianhu)
                        {
                            TblDingdan.Rows[2].Cells[3].Text += model.Xingming;
                            TblDingdan.Rows[2].Cells[3].Text += "<input class=\"BtnModify\" type=\"button\" onclick=\"location='../huiqianhup/Modify.aspx?Index=" + model.Index.ToString() + "'\"/>";
                            TblDingdan.Rows[2].Cells[3].Text += "<input class=\"BtnDelete\" type=\"button\" onclick=\"location='../huiqianhup/Delete.aspx?Index=" + model.Index.ToString() + "&Fangyuan=" + fangyuan.Index.ToString() + "'\"/>";
                            TblDingdan.Rows[2].Cells[3].Text += "<br/>";
                        }
                        TblDingdan.Rows[2].Cells[4].Text = "";
                        foreach (Huiqiandianhua model in dianhua)
                        {
                            TblDingdan.Rows[2].Cells[4].Text += model.Dianhua;
                            TblDingdan.Rows[2].Cells[4].Text += "<input class=\"BtnModify\" type=\"button\" onclick=\"location='../huiqiandianhuap/Modify.aspx?Index=" + model.Index.ToString() + "'\"/>";
                            TblDingdan.Rows[2].Cells[4].Text += "<input class=\"BtnDelete\" type=\"button\" onclick=\"location='../huiqiandianhuap/Delete.aspx?Index=" + model.Index.ToString() + "'\"/>";
                            TblDingdan.Rows[2].Cells[4].Text += "<br/>";
                        }
                        TblDingdan.Rows[2].Cells[5].Text = dingdan.Zongjia.Value.ToString("c0");
                        string[] paymode = { "全款", "分期", "贷款" };
                        TblDingdan.Rows[2].Cells[6].Text = paymode[dingdan.PaymentMode.Value];
                        if (dingdan.Dingjin != null)
                            TblDingdan.Rows[2].Cells[7].Text = dingdan.Dingjin.Value.ToString("C0");

                        if (dingdan.BukuanDate != null)
                            TblDingdan.Rows[2].Cells[10].Text = dingdan.BukuanDate.Value.ToString("yyyy-MM-dd");
                        if (dingdan.SellerIndex > 0)
                            TblDingdan.Rows[2].Cells[11].Text = yuangong.Name;
                        TblDingdan.Rows[2].Cells[12].Text = dingdan.Beizhu;

                        if (dingdan.PaymentMode == 0 )
                        {
                            TblDingdan.Rows[1].Cells.RemoveAt(9);
                            TblDingdan.Rows[1].Cells.RemoveAt(8);
                            TblDingdan.Rows[2].Cells.RemoveAt(9);
                            TblDingdan.Rows[2].Cells.RemoveAt(8);
                            TblDingdan.Rows[0].Cells[0].ColumnSpan = 9;
                        }
                        else if (dingdan.PaymentMode==1)
                        {
                            TblDingdan.Rows[1].Cells[8].Text = "分期月数";
                            TblDingdan.Rows[1].Cells[9].Text = "月还款额";
                            TblDingdan.Rows[2].Cells[8].Text = dingdan.Fenqi.ToString();
                            TblDingdan.Rows[2].Cells[9].Text = ((dingdan.Zongjia - dingdan.Dingjin) / dingdan.Fenqi).ToString();
                        }
                        else
                        {
                            TblDingdan.Rows[2].Cells[8].Text = dingdan.Shoufu.Value.ToString("C0");
                            TblDingdan.Rows[2].Cells[9].Text = dingdan.Daikuan.Value.ToString("C0");
                        }

                        TblDingdan.Visible = true;
                    }
                    else if (fangyuan.State == 3)
                    {
                        TblXieyi.Visible = true;
                        TblShoukuan.Visible = false;
                        huiqianxieyi = HuiqianxieyiBLL.GetDataByIndex(fangyuan.XieyiIndex.Value);
                        TblXieyi.Rows[2].Cells[0].Text = huiqianxieyi.Index.ToString();
                        foreach (Huiqianhu model in huiqianhu)
                            TblXieyi.Rows[2].Cells[1].Text += model.Xingming + "<br/>";
                        foreach (Huiqiandianhua model in dianhua)
                            TblXieyi.Rows[2].Cells[2].Text += model.Dianhua + "<br/>";

                        //Response.Redirect("../huiqianxieyip/Detail.aspx?Index=" + fangyuan.XieyiIndex);
                        //HuiqianxieyiBLL.GetDataByXieyihao(Convert.ToInt32(fangyuan.XieyiIndex));
                    }
                }

                // 物业费记录
                wuyefei = WuyefeiBLL.GetDataByFangyuanIndex(fangyuan.Index);
                RepGrid.DataSource = wuyefei;
                RepGrid.DataBind();
            }
            TimeSpan ts = DateTime.Now.Subtract(dt);
            LblCostTime.Text = "耗时" + ts.TotalMilliseconds.ToString() + "ms";
        }

        /// <summary>
        /// 用户单击“保存”按钮时的事件处理方法。
        /// </summary>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            //Main.SysMessage.ShowSuccessMsg("“员工”修改成功，单击“确定”返回信息列表。", "Default.aspx");
        }

        protected void BtnDingfang_Click(object sender, EventArgs e)
        {
            Response.Redirect("../dingdanp/Create.aspx?Index=" + fangyuan.Index.ToString());
        }

        protected void BtnTuifang_Click(object sender, EventArgs e)
        {
            //  需要将销售里面的收费退掉
            Response.Redirect("../dingdanp/Delete.aspx?Index=" + dingdan.Index.ToString());
        }
    }
}
